import datetime
import pandas as pd
import numpy as np
nan=np.nan
inf=np.inf
def get_str(i,starts,ends):
    n0 = i.find(starts)
    n1 = "".join(reversed(i)).find(ends)
    if n0 == -1 or n1 == -1:
        return None
    n1 = len(i) - n1
    stri = i[n0:n1]
    return stri
def log2dict(logfile,self_func=None,filter_func=None,keys=[],del_keys=[],add_data={},filter_check_len=None):
    with open(logfile, "r", encoding="utf8") as f:
        for i in f:
            stri=get_str(i,"(",")")
            if stri is None:
                continue
            sep=stri[:5]
            aa=stri.split(sep)[1:]
            if len(aa)>=2:
                print("一行包含多个记录")
            while aa:
                tt=sep+aa.pop()
                stri = get_str(tt, "(", ")")
                if stri   is None:
                    continue
                if self_func is not None:
                    stri=self_func(stri)
                # a = eval(stri)
                try:
                    a = eval(stri)
                except Exception as e:
                    print(e,stri)
                    continue
                if filter_check_len is not None:
                    if len(a)<filter_check_len:
                        continue
                    elif len(a)>filter_check_len:
                        a=a[:filter_check_len]
                if filter_func:
                    if not filter_func(a):
                        continue
                d={}
                n=0
                for dd in a:
                    if isinstance(dd,dict):
                        d.update(dd)
                    else:
                        if n<len(keys):
                            d[keys[n]]=dd
                        n=n+1
                for k in  del_keys:
                   del d[k]
                d.update(add_data)
                yield d
                # if key is None:
                #     data.append(d)
                # else:
                #     k=d[key]
                #     data[k]=d

if __name__ == '__main__':
    def filter0(x):
        if x[2] == "正向":
            return True
        else:
            return False
    data=log2dict(r"F:\ss\xingtai_CDLHIKKAKE\xingtai.log",keys=["sys","key","reverse_bs"],del_keys=["sys"])
    print("end")